/**
 * ProjectName renren-security
 * FileName OssUploader.java
 * PackageName com.baidu.ueditor.upload
 * Date 2017年4月10日 上午10:12:44
 * Copyright (c) 2015, lyg945@163.com All Rights Reserved.
 */
package com.baidu.ueditor.upload;

import io.renren.utils.OSSClientUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.util.StringUtils;

import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.FileType;
import com.baidu.ueditor.define.State;

public final class OssUploader {

	public static final State save(HttpServletRequest request,Map<String, Object> conf) {
		
		File file = new File("d:\\a.jpg");
		byte[] data =new byte[512]; 
		 int numberRead=0;  
		 FileInputStream input=null;  
	     FileOutputStream out =null; 
	     
		 try {
		        out=new FileOutputStream("D:/tiger2.jpg"); //如果文件不存在会自动创建  	
		        
			while ((numberRead=request.getInputStream().read(data))!=-1) {  //numberRead的目的在于防止最后一次读取的字节小于buffer长度，  
			       out.write(data, 0, numberRead);       //否则会自动被填充0  
			    }
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}  
		
//		try {
//			FileUtils.writeByteArrayToFile(file, data);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		FileItemStream fileStream = null;
//		boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
//
//		if (!ServletFileUpload.isMultipartContent(request)) {
//			return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
//		}
//
//		ServletFileUpload upload = new ServletFileUpload(
//				new DiskFileItemFactory());
//
//        if ( isAjaxUpload ) {
//            upload.setHeaderEncoding( "UTF-8" );
//        }
//
//		try {
//			FileItemIterator iterator = upload.getItemIterator(request);
//
//			while (iterator.hasNext()) {
//				fileStream = iterator.next();
//
//				if (!fileStream.isFormField())
//					break;
//				fileStream = null;
//			}
//
//			if (fileStream == null) {
//				return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
//			}
//
////			String savePath = (String) conf.get("savePath");
//			String originFileName = fileStream.getName();
//			String suffix = FileType.getSuffixByFilename(originFileName);
////
////			originFileName = originFileName.substring(0,
////					originFileName.length() - suffix.length());
////			savePath = savePath + suffix;
//
//			long maxSize = ((Long) conf.get("maxSize")).longValue();
//
//			if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
//				return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
//			}
//
////			savePath = PathFormat.parse(savePath, originFileName);
//
////			String physicalPath = (String) conf.get("rootPath") + savePath;
//
//			InputStream is = fileStream.openStream();
////			State storageState = StorageManager.saveFileByInputStream(is,
////					physicalPath, maxSize);
//			is.close();
//			
//			State storageState = new BaseState(true);
//			
//			OSSClientUtil oss = new OSSClientUtil();
//			String url = oss.uploadFile2OSS(request.getInputStream(), originFileName);
//			if(StringUtils.hasText(url)){
//				storageState.putInfo("url", originFileName);
//				storageState.putInfo("type", suffix);
//				storageState.putInfo("original", originFileName + suffix);
//			}
//
////			if (storageState.isSuccess()) {
////				storageState.putInfo("url", PathFormat.format(savePath));
////				storageState.putInfo("type", suffix);
////				storageState.putInfo("original", originFileName + suffix);
////			}
//
//			return storageState;
//		} catch (FileUploadException e) {
//			return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
//		} catch (IOException e) {
//		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}

	private static boolean validType(String type, String[] allowTypes) {
		List<String> list = Arrays.asList(allowTypes);

		return list.contains(type);
	}
}
